<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- samegame.qdoc -->
  <title>Qt Quick Demo - Same Game | Qt 5.14</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.14</a></td><td >Qt Quick Demo - Same Game</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.14.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<h1 class="title">Qt Quick Demo - Same Game</h1>
<span class="subtitle"></span>
<pre class="js">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2017 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="comment">/* This script file handles the game logic */</span>
  .pragma library
  .import QtQuick.LocalStorage 2.0 as Sql

  var maxColumn = <span class="number">10</span>;
  var maxRow = <span class="number">13</span>;
  var types = <span class="number">3</span>;
  var maxIndex = <span class="name">maxColumn</span><span class="operator">*</span><span class="name">maxRow</span>;
  var board = new <span class="name">Array</span>(<span class="name">maxIndex</span>);
  var blockSrc = <span class="string">&quot;Block.qml&quot;</span>;
  var gameDuration;
  var component = <span class="name">Qt</span>.<span class="name">createComponent</span>(<span class="name">blockSrc</span>);
  var gameCanvas;
  var betweenTurns = <span class="number">false</span>;

  var puzzleLevel = <span class="number">null</span>;
  var puzzlePath = <span class="string">&quot;&quot;</span>;

  var gameMode = <span class="string">&quot;arcade&quot;</span>; <span class="comment">//Set in new game, then tweaks behaviour of other functions</span>
  var gameOver = <span class="number">false</span>;

  <span class="keyword">function</span> <span class="name">changeBlock</span>(src)
  {
      <span class="name">blockSrc</span> <span class="operator">=</span> <span class="name">src</span>;
      <span class="name">component</span> <span class="operator">=</span> <span class="name">Qt</span>.<span class="name">createComponent</span>(<span class="name">blockSrc</span>);
  }

  <span class="comment">// Index function used instead of a 2D array</span>
  <span class="keyword">function</span> <span class="name">index</span>(column, row)
  {
      <span class="keyword">return</span> <span class="name">column</span> <span class="operator">+</span> <span class="name">row</span> <span class="operator">*</span> <span class="name">maxColumn</span>;
  }

  <span class="keyword">function</span> <span class="name">timeStr</span>(msecs)
  {
      var secs = <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">msecs</span><span class="operator">/</span><span class="number">1000</span>);
      var m = <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">secs</span><span class="operator">/</span><span class="number">60</span>);
      var ret = <span class="string">&quot;&quot;</span> <span class="operator">+</span> <span class="name">m</span> <span class="operator">+</span> <span class="string">&quot;m &quot;</span> <span class="operator">+</span> (<span class="name">secs</span><span class="operator">%</span><span class="number">60</span>) <span class="operator">+</span> <span class="string">&quot;s&quot;</span>;
      <span class="keyword">return</span> <span class="name">ret</span>;
  }

  <span class="keyword">function</span> <span class="name">cleanUp</span>()
  {
      <span class="keyword">if</span> (<span class="name">gameCanvas</span> <span class="operator">==</span> <span class="name">undefined</span>)
          <span class="keyword">return</span>;
      <span class="comment">// Delete blocks from previous game</span>
      <span class="keyword">for</span> (var i = 0; <span class="name">i</span> <span class="operator">&lt;</span> <span class="name">maxIndex</span>; i++) {
          <span class="keyword">if</span> (<span class="name">board</span>[<span class="name">i</span>] <span class="operator">!=</span> <span class="number">null</span>)
              <span class="name">board</span>[<span class="name">i</span>].<span class="name">destroy</span>();
          <span class="name">board</span>[<span class="name">i</span>] <span class="operator">=</span> <span class="number">null</span>;
      }
      <span class="keyword">if</span> (<span class="name">puzzleLevel</span> <span class="operator">!=</span> <span class="number">null</span>){
          <span class="name">puzzleLevel</span>.<span class="name">destroy</span>();
          <span class="name">puzzleLevel</span> <span class="operator">=</span> <span class="number">null</span>;
      }
      <span class="name">gameCanvas</span>.<span class="name">mode</span> <span class="operator">=</span> <span class="string">&quot;&quot;</span>
  }

  <span class="keyword">function</span> <span class="name">startNewGame</span>(gc, mode, map)
  {
      <span class="name">gameCanvas</span> <span class="operator">=</span> <span class="name">gc</span>;
      <span class="keyword">if</span> (<span class="name">mode</span> <span class="operator">==</span> <span class="name">undefined</span>)
          <span class="name">gameMode</span> <span class="operator">=</span> <span class="string">&quot;arcade&quot;</span>;
      <span class="keyword">else</span>
          <span class="name">gameMode</span> <span class="operator">=</span> <span class="name">mode</span>;
      <span class="name">gameOver</span> <span class="operator">=</span> <span class="number">false</span>;

      <span class="name">cleanUp</span>();

      <span class="name">gc</span>.<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">false</span>;
      <span class="name">gc</span>.<span class="name">mode</span> <span class="operator">=</span> <span class="name">gameMode</span>;
      <span class="comment">// Calculate board size</span>
      <span class="name">maxColumn</span> <span class="operator">=</span> <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">gameCanvas</span>.<span class="name">width</span><span class="operator">/</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>);
      <span class="name">maxRow</span> <span class="operator">=</span> <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">gameCanvas</span>.<span class="name">height</span><span class="operator">/</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>);
      <span class="name">maxIndex</span> <span class="operator">=</span> <span class="name">maxRow</span> <span class="operator">*</span> <span class="name">maxColumn</span>;
      <span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">&quot;arcade&quot;</span>) <span class="comment">//Needs to be after board sizing</span>
          <span class="name">getHighScore</span>();

      <span class="comment">// Initialize Board</span>
      <span class="name">board</span> <span class="operator">=</span> new <span class="name">Array</span>(<span class="name">maxIndex</span>);
      <span class="name">gameCanvas</span>.<span class="name">score</span> <span class="operator">=</span> <span class="number">0</span>;
      <span class="name">gameCanvas</span>.<span class="name">score2</span> <span class="operator">=</span> <span class="number">0</span>;
      <span class="name">gameCanvas</span>.<span class="name">moves</span> <span class="operator">=</span> <span class="number">0</span>;
      <span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">=</span> <span class="number">1</span>;
      <span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">&quot;puzzle&quot;</span>)
          <span class="name">loadMap</span>(<span class="name">map</span>);
      <span class="keyword">else</span><span class="comment">//Note that we load them in reverse order for correct visual stacking</span>
          <span class="keyword">for</span> (var column = maxColumn - 1; <span class="name">column</span> <span class="operator">&gt;=</span> <span class="number">0</span>; column--)
              <span class="keyword">for</span> (var row = maxRow - 1; <span class="name">row</span> <span class="operator">&gt;=</span> <span class="number">0</span>; row--)
                  <span class="name">createBlock</span>(<span class="name">column</span>, <span class="name">row</span>);
      <span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">&quot;puzzle&quot;</span>)
          <span class="name">getLevelHistory</span>();<span class="comment">//Needs to be after map load</span>
      <span class="name">gameDuration</span> <span class="operator">=</span> new <span class="name">Date</span>();
  }

  var fillFound;  <span class="comment">// Set after a floodFill call to the number of blocks found</span>
  var floodBoard; <span class="comment">// Set to 1 if the floodFill reaches off that node</span>

  <span class="comment">// NOTE: Be careful with vars named x,y, as the calling object's x,y are still in scope</span>
  <span class="keyword">function</span> <span class="name">handleClick</span>(x,y)
  {
      <span class="keyword">if</span> (<span class="name">betweenTurns</span> <span class="operator">||</span> <span class="name">gameOver</span> <span class="operator">||</span> <span class="name">gameCanvas</span> <span class="operator">==</span> <span class="name">undefined</span>)
          <span class="keyword">return</span>;
      var column = <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">x</span><span class="operator">/</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>);
      var row = <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">y</span><span class="operator">/</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>);
      <span class="keyword">if</span> (<span class="name">column</span> <span class="operator">&gt;=</span> <span class="name">maxColumn</span> <span class="operator">||</span> <span class="name">column</span> <span class="operator">&lt;</span> <span class="number">0</span> <span class="operator">||</span> <span class="name">row</span> <span class="operator">&gt;=</span> <span class="name">maxRow</span> <span class="operator">||</span> <span class="name">row</span> <span class="operator">&lt;</span> <span class="number">0</span>)
          <span class="keyword">return</span>;
      <span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">==</span> <span class="number">null</span>)
          <span class="keyword">return</span>;
      <span class="comment">// If it's a valid block, remove it and all connected (does nothing if it's not connected)</span>
      <span class="name">floodFill</span>(<span class="name">column</span>,<span class="name">row</span>, -<span class="number">1</span>);
      <span class="keyword">if</span> (<span class="name">fillFound</span> <span class="operator">&lt;=</span> <span class="number">0</span>)
          <span class="keyword">return</span>;
      <span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">&quot;multiplayer&quot;</span> <span class="operator">&amp;&amp;</span> <span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">==</span> <span class="number">2</span>)
          <span class="name">gameCanvas</span>.<span class="name">score2</span> <span class="operator">+=</span> (<span class="name">fillFound</span> <span class="operator">-</span> <span class="number">1</span>) <span class="operator">*</span> (<span class="name">fillFound</span> <span class="operator">-</span> <span class="number">1</span>);
      <span class="keyword">else</span>
          <span class="name">gameCanvas</span>.<span class="name">score</span> <span class="operator">+=</span> (<span class="name">fillFound</span> <span class="operator">-</span> <span class="number">1</span>) <span class="operator">*</span> (<span class="name">fillFound</span> <span class="operator">-</span> <span class="number">1</span>);
      <span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">&quot;multiplayer&quot;</span> <span class="operator">&amp;&amp;</span> <span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">==</span> <span class="number">2</span>)
          <span class="name">shuffleUp</span>();
      <span class="keyword">else</span>
          <span class="name">shuffleDown</span>();
      <span class="name">gameCanvas</span>.<span class="name">moves</span> <span class="operator">+=</span> <span class="number">1</span>;
      <span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">&quot;endless&quot;</span>)
          <span class="name">refill</span>();
      <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">!=</span> <span class="string">&quot;multiplayer&quot;</span>)
          <span class="name">victoryCheck</span>();
      <span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">&quot;multiplayer&quot;</span> <span class="operator">&amp;&amp;</span> !<span class="name">gc</span>.<span class="name">gameOver</span>){
          <span class="name">betweenTurns</span> <span class="operator">=</span> <span class="number">true</span>;
          <span class="name">gameCanvas</span>.<span class="name">swapPlayers</span>();<span class="comment">//signal, animate and call turnChange() when ready</span>
      }
  }

  <span class="keyword">function</span> <span class="name">floodFill</span>(column,row,type)
  {
      <span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">==</span> <span class="number">null</span>)
          <span class="keyword">return</span>;
      var first = <span class="number">false</span>;
      <span class="keyword">if</span> (<span class="name">type</span> <span class="operator">==</span> -<span class="number">1</span>) {
          <span class="name">first</span> <span class="operator">=</span> <span class="number">true</span>;
          <span class="name">type</span> <span class="operator">=</span> <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>,<span class="name">row</span>)].<span class="name">type</span>;

          <span class="comment">// Flood fill initialization</span>
          <span class="name">fillFound</span> <span class="operator">=</span> <span class="number">0</span>;
          <span class="name">floodBoard</span> <span class="operator">=</span> new <span class="name">Array</span>(<span class="name">maxIndex</span>);
      }
      <span class="keyword">if</span> (<span class="name">column</span> <span class="operator">&gt;=</span> <span class="name">maxColumn</span> <span class="operator">||</span> <span class="name">column</span> <span class="operator">&lt;</span> <span class="number">0</span> <span class="operator">||</span> <span class="name">row</span> <span class="operator">&gt;=</span> <span class="name">maxRow</span> <span class="operator">||</span> <span class="name">row</span> <span class="operator">&lt;</span> <span class="number">0</span>)
          <span class="keyword">return</span>;
      <span class="keyword">if</span> (<span class="name">floodBoard</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">==</span> <span class="number">1</span> <span class="operator">||</span> (!<span class="name">first</span> <span class="operator">&amp;&amp;</span> <span class="name">type</span> <span class="operator">!=</span> <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)].<span class="name">type</span>))
          <span class="keyword">return</span>;
      <span class="name">floodBoard</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">=</span> <span class="number">1</span>;
      <span class="name">floodFill</span>(<span class="name">column</span> <span class="operator">+</span> <span class="number">1</span>, <span class="name">row</span>, <span class="name">type</span>);
      <span class="name">floodFill</span>(<span class="name">column</span> <span class="operator">-</span> <span class="number">1</span>, <span class="name">row</span>, <span class="name">type</span>);
      <span class="name">floodFill</span>(<span class="name">column</span>, <span class="name">row</span> <span class="operator">+</span> <span class="number">1</span>, <span class="name">type</span>);
      <span class="name">floodFill</span>(<span class="name">column</span>, <span class="name">row</span> <span class="operator">-</span> <span class="number">1</span>, <span class="name">type</span>);
      <span class="keyword">if</span> (<span class="name">first</span> <span class="operator">==</span> <span class="number">true</span> <span class="operator">&amp;&amp;</span> <span class="name">fillFound</span> <span class="operator">==</span> <span class="number">0</span>)
          <span class="keyword">return</span>; <span class="comment">// Can't remove single blocks</span>
      <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)].<span class="name">dying</span> <span class="operator">=</span> <span class="number">true</span>;
      <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">=</span> <span class="number">null</span>;
      <span class="name">fillFound</span> <span class="operator">+=</span> <span class="number">1</span>;
  }

  <span class="keyword">function</span> <span class="name">shuffleDown</span>()
  {
      <span class="comment">// Fall down</span>
      <span class="keyword">for</span> (var column = 0; <span class="name">column</span> <span class="operator">&lt;</span> <span class="name">maxColumn</span>; column++) {
          var fallDist = <span class="number">0</span>;
          <span class="keyword">for</span> (var row = maxRow - 1; <span class="name">row</span> <span class="operator">&gt;=</span> <span class="number">0</span>; row--) {
              <span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>,<span class="name">row</span>)] <span class="operator">==</span> <span class="number">null</span>) {
                  <span class="name">fallDist</span> <span class="operator">+=</span> <span class="number">1</span>;
              } <span class="keyword">else</span> {
                  <span class="keyword">if</span> (<span class="name">fallDist</span> <span class="operator">&gt;</span> <span class="number">0</span>) {
                      var obj = <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)];
                      <span class="name">obj</span>.<span class="name">y</span> <span class="operator">=</span> (<span class="name">row</span> <span class="operator">+</span> <span class="name">fallDist</span>) <span class="operator">*</span> <span class="name">gameCanvas</span>.<span class="name">blockSize</span>;
                      <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span> <span class="operator">+</span> <span class="name">fallDist</span>)] <span class="operator">=</span> <span class="name">obj</span>;
                      <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">=</span> <span class="number">null</span>;
                  }
              }
          }
      }
      <span class="comment">// Fall to the left</span>
      <span class="name">fallDist</span> <span class="operator">=</span> <span class="number">0</span>;
      <span class="keyword">for</span> (<span class="name">column</span> <span class="operator">=</span> <span class="number">0</span>; <span class="name">column</span> <span class="operator">&lt;</span> <span class="name">maxColumn</span>; column++) {
          <span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">maxRow</span> <span class="operator">-</span> <span class="number">1</span>)] <span class="operator">==</span> <span class="number">null</span>) {
              <span class="name">fallDist</span> <span class="operator">+=</span> <span class="number">1</span>;
          } <span class="keyword">else</span> {
              <span class="keyword">if</span> (<span class="name">fallDist</span> <span class="operator">&gt;</span> <span class="number">0</span>) {
                  <span class="keyword">for</span> (<span class="name">row</span> <span class="operator">=</span> <span class="number">0</span>; <span class="name">row</span> <span class="operator">&lt;</span> <span class="name">maxRow</span>; row++) {
                      <span class="name">obj</span> <span class="operator">=</span> <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)];
                      <span class="keyword">if</span> (<span class="name">obj</span> <span class="operator">==</span> <span class="number">null</span>)
                          <span class="keyword">continue</span>;
                      <span class="name">obj</span>.<span class="name">x</span> <span class="operator">=</span> (<span class="name">column</span> <span class="operator">-</span> <span class="name">fallDist</span>) <span class="operator">*</span> <span class="name">gameCanvas</span>.<span class="name">blockSize</span>;
                      <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span> <span class="operator">-</span> <span class="name">fallDist</span>,<span class="name">row</span>)] <span class="operator">=</span> <span class="name">obj</span>;
                      <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">=</span> <span class="number">null</span>;
                  }
              }
          }
      }
  }

  <span class="keyword">function</span> <span class="name">shuffleUp</span>()
  {
      <span class="comment">// Fall up</span>
      <span class="keyword">for</span> (var column = 0; <span class="name">column</span> <span class="operator">&lt;</span> <span class="name">maxColumn</span>; column++) {
          var fallDist = <span class="number">0</span>;
          <span class="keyword">for</span> (var row = 0; <span class="name">row</span> <span class="operator">&lt;</span> <span class="name">maxRow</span>; row++) {
              <span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>,<span class="name">row</span>)] <span class="operator">==</span> <span class="number">null</span>) {
                  <span class="name">fallDist</span> <span class="operator">+=</span> <span class="number">1</span>;
              } <span class="keyword">else</span> {
                  <span class="keyword">if</span> (<span class="name">fallDist</span> <span class="operator">&gt;</span> <span class="number">0</span>) {
                      var obj = <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)];
                      <span class="name">obj</span>.<span class="name">y</span> <span class="operator">=</span> (<span class="name">row</span> <span class="operator">-</span> <span class="name">fallDist</span>) <span class="operator">*</span> <span class="name">gameCanvas</span>.<span class="name">blockSize</span>;
                      <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span> <span class="operator">-</span> <span class="name">fallDist</span>)] <span class="operator">=</span> <span class="name">obj</span>;
                      <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">=</span> <span class="number">null</span>;
                  }
              }
          }
      }
      <span class="comment">// Fall to the left (or should it be right, so as to be left for P2?)</span>
      <span class="name">fallDist</span> <span class="operator">=</span> <span class="number">0</span>;
      <span class="keyword">for</span> (<span class="name">column</span> <span class="operator">=</span> <span class="number">0</span>; <span class="name">column</span> <span class="operator">&lt;</span> <span class="name">maxColumn</span>; column++) {
          <span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="number">0</span>)] <span class="operator">==</span> <span class="number">null</span>) {
              <span class="name">fallDist</span> <span class="operator">+=</span> <span class="number">1</span>;
          } <span class="keyword">else</span> {
              <span class="keyword">if</span> (<span class="name">fallDist</span> <span class="operator">&gt;</span> <span class="number">0</span>) {
                  <span class="keyword">for</span> (<span class="name">row</span> <span class="operator">=</span> <span class="number">0</span>; <span class="name">row</span> <span class="operator">&lt;</span> <span class="name">maxRow</span>; row++) {
                      <span class="name">obj</span> <span class="operator">=</span> <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)];
                      <span class="keyword">if</span> (<span class="name">obj</span> <span class="operator">==</span> <span class="number">null</span>)
                          <span class="keyword">continue</span>;
                      <span class="name">obj</span>.<span class="name">x</span> <span class="operator">=</span> (<span class="name">column</span> <span class="operator">-</span> <span class="name">fallDist</span>) <span class="operator">*</span> <span class="name">gameCanvas</span>.<span class="name">blockSize</span>;
                      <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span> <span class="operator">-</span> <span class="name">fallDist</span>,<span class="name">row</span>)] <span class="operator">=</span> <span class="name">obj</span>;
                      <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">=</span> <span class="number">null</span>;
                  }
              }
          }
      }
  }

  <span class="keyword">function</span> <span class="name">turnChange</span>()<span class="comment">//called by ui outside</span>
  {
      <span class="name">betweenTurns</span> <span class="operator">=</span> <span class="number">false</span>;
      <span class="keyword">if</span> (<span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">==</span> <span class="number">1</span>){
          <span class="name">shuffleUp</span>();
          <span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">=</span> <span class="number">2</span>;
          <span class="name">victoryCheck</span>();
      }<span class="keyword">else</span>{
          <span class="name">shuffleDown</span>();
          <span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">=</span> <span class="number">1</span>;
          <span class="name">victoryCheck</span>();
      }
  }

  <span class="keyword">function</span> <span class="name">refill</span>()
  {
      <span class="keyword">for</span> (var column = 0; <span class="name">column</span> <span class="operator">&lt;</span> <span class="name">maxColumn</span>; column++) {
          <span class="keyword">for</span> (var row = 0; <span class="name">row</span> <span class="operator">&lt;</span> <span class="name">maxRow</span>; row++) {
              <span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">==</span> <span class="number">null</span>)
                  <span class="name">createBlock</span>(<span class="name">column</span>, <span class="name">row</span>);
          }
      }
  }

  <span class="keyword">function</span> <span class="name">victoryCheck</span>()
  {
      <span class="comment">// Awards bonuses for no blocks left</span>
      var deservesBonus = <span class="number">true</span>;
      <span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="number">0</span>,<span class="name">maxRow</span> <span class="operator">-</span> <span class="number">1</span>)] <span class="operator">!=</span> <span class="number">null</span> <span class="operator">||</span> <span class="name">board</span>[<span class="name">index</span>(<span class="number">0</span>,<span class="number">0</span>)] <span class="operator">!=</span> <span class="number">null</span>)
          <span class="name">deservesBonus</span> <span class="operator">=</span> <span class="number">false</span>;
      <span class="comment">// Checks for game over</span>
      <span class="keyword">if</span> (<span class="name">deservesBonus</span>){
          <span class="keyword">if</span> (<span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">=</span> <span class="number">1</span>)
              <span class="name">gameCanvas</span>.<span class="name">score</span> <span class="operator">+=</span> <span class="number">1000</span>;
          <span class="keyword">else</span>
              <span class="name">gameCanvas</span>.<span class="name">score2</span> <span class="operator">+=</span> <span class="number">1000</span>;
      }
      <span class="name">gameOver</span> <span class="operator">=</span> <span class="name">deservesBonus</span>;
      <span class="keyword">if</span> (<span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">==</span> <span class="number">1</span>){
          <span class="keyword">if</span> (!(<span class="name">floodMoveCheck</span>(<span class="number">0</span>, <span class="name">maxRow</span> <span class="operator">-</span> <span class="number">1</span>, -<span class="number">1</span>)))
              <span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
      }<span class="keyword">else</span>{
          <span class="keyword">if</span> (!(<span class="name">floodMoveCheck</span>(<span class="number">0</span>, <span class="number">0</span>, -<span class="number">1</span>, <span class="number">true</span>)))
              <span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
      }
      <span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">&quot;puzzle&quot;</span>){
          <span class="name">puzzleVictoryCheck</span>(<span class="name">deservesBonus</span>);<span class="comment">//Takes it from here</span>
          <span class="keyword">return</span>;
      }
      <span class="keyword">if</span> (<span class="name">gameOver</span>) {
          var winnerScore = <span class="name">Math</span>.<span class="name">max</span>(<span class="name">gameCanvas</span>.<span class="name">score</span>, <span class="name">gameCanvas</span>.<span class="name">score2</span>);
          <span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">&quot;multiplayer&quot;</span>){
              <span class="name">gameCanvas</span>.<span class="name">score</span> <span class="operator">=</span> <span class="name">winnerScore</span>;
              <span class="name">saveHighScore</span>(<span class="name">gameCanvas</span>.<span class="name">score2</span>);
          }
          <span class="name">saveHighScore</span>(<span class="name">gameCanvas</span>.<span class="name">score</span>);
          <span class="name">gameDuration</span> <span class="operator">=</span> new <span class="name">Date</span>() <span class="operator">-</span> <span class="name">gameDuration</span>;
          <span class="name">gameCanvas</span>.<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
      }
  }

  <span class="comment">// Only floods up and right, to see if it can find adjacent same-typed blocks</span>
  <span class="keyword">function</span> <span class="name">floodMoveCheck</span>(column, row, type, goDownInstead)
  {
      <span class="keyword">if</span> (<span class="name">column</span> <span class="operator">&gt;=</span> <span class="name">maxColumn</span> <span class="operator">||</span> <span class="name">column</span> <span class="operator">&lt;</span> <span class="number">0</span> <span class="operator">||</span> <span class="name">row</span> <span class="operator">&gt;=</span> <span class="name">maxRow</span> <span class="operator">||</span> <span class="name">row</span> <span class="operator">&lt;</span> <span class="number">0</span>)
          <span class="keyword">return</span> <span class="number">false</span>;
      <span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)] <span class="operator">==</span> <span class="number">null</span>)
          <span class="keyword">return</span> <span class="number">false</span>;
      var myType = <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>, <span class="name">row</span>)].<span class="name">type</span>;
      <span class="keyword">if</span> (<span class="name">type</span> <span class="operator">==</span> <span class="name">myType</span>)
          <span class="keyword">return</span> <span class="number">true</span>;
      <span class="keyword">if</span> (<span class="name">goDownInstead</span>)
          <span class="keyword">return</span> <span class="name">floodMoveCheck</span>(<span class="name">column</span> <span class="operator">+</span> <span class="number">1</span>, <span class="name">row</span>, <span class="name">myType</span>, <span class="name">goDownInstead</span>) <span class="operator">||</span>
                 <span class="name">floodMoveCheck</span>(<span class="name">column</span>, <span class="name">row</span> <span class="operator">+</span> <span class="number">1</span>, <span class="name">myType</span>, <span class="name">goDownInstead</span>);
      <span class="keyword">else</span>
          <span class="keyword">return</span> <span class="name">floodMoveCheck</span>(<span class="name">column</span> <span class="operator">+</span> <span class="number">1</span>, <span class="name">row</span>, <span class="name">myType</span>) <span class="operator">||</span>
                 <span class="name">floodMoveCheck</span>(<span class="name">column</span>, <span class="name">row</span> <span class="operator">-</span> <span class="number">1</span>, <span class="name">myType</span>);
  }

  <span class="keyword">function</span> <span class="name">createBlock</span>(column,row,type)
  {
      <span class="comment">// Note that we don't wait for the component to become ready. This will</span>
      <span class="comment">// only work if the block QML is a local file. Otherwise the component will</span>
      <span class="comment">// not be ready immediately. There is a statusChanged signal on the</span>
      <span class="comment">// component you could use if you want to wait to load remote files.</span>
      <span class="keyword">if</span> (<span class="name">component</span>.<span class="name">status</span> <span class="operator">==</span> <span class="number">1</span>){
          <span class="keyword">if</span> (<span class="name">type</span> <span class="operator">==</span> <span class="name">undefined</span>)
              <span class="name">type</span> <span class="operator">=</span> <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="name">types</span>);
          <span class="keyword">if</span> (<span class="name">type</span> <span class="operator">&lt;</span> <span class="number">0</span> <span class="operator">||</span> <span class="name">type</span> <span class="operator">&gt;</span> <span class="number">4</span>) {
              <span class="name">console</span>.<span class="name">log</span>(<span class="string">&quot;Invalid type requested&quot;</span>);<span class="comment">//TODO: Is this triggered by custom levels much?</span>
              <span class="keyword">return</span>;
          }
          var dynamicObject = <span class="name">component</span>.<span class="name">createObject</span>(<span class="name">gameCanvas</span>,
                  {&quot;type&quot;: <span class="name">type</span>,
                  &quot;x&quot;: <span class="name">column</span><span class="operator">*</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>,
                  &quot;y&quot;: -<span class="number">1</span><span class="operator">*</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>,
                  &quot;width&quot;: <span class="name">gameCanvas</span>.<span class="name">blockSize</span>,
                  &quot;height&quot;: <span class="name">gameCanvas</span>.<span class="name">blockSize</span>,
                  &quot;particleSystem&quot;: <span class="name">gameCanvas</span>.<span class="name">ps</span>});
          <span class="keyword">if</span> (<span class="name">dynamicObject</span> <span class="operator">==</span> <span class="number">null</span>){
              <span class="name">console</span>.<span class="name">log</span>(<span class="string">&quot;error creating block&quot;</span>);
              <span class="name">console</span>.<span class="name">log</span>(<span class="name">component</span>.<span class="name">errorString</span>());
              <span class="keyword">return</span> <span class="number">false</span>;
          }
          <span class="name">dynamicObject</span>.<span class="name">y</span> <span class="operator">=</span> <span class="name">row</span><span class="operator">*</span><span class="name">gameCanvas</span>.<span class="name">blockSize</span>;
          <span class="name">dynamicObject</span>.<span class="name">spawned</span> <span class="operator">=</span> <span class="number">true</span>;

          <span class="name">board</span>[<span class="name">index</span>(<span class="name">column</span>,<span class="name">row</span>)] <span class="operator">=</span> <span class="name">dynamicObject</span>;
      }<span class="keyword">else</span>{
          <span class="name">console</span>.<span class="name">log</span>(<span class="string">&quot;error loading block component&quot;</span>);
          <span class="name">console</span>.<span class="name">log</span>(<span class="name">component</span>.<span class="name">errorString</span>());
          <span class="keyword">return</span> <span class="number">false</span>;
      }
      <span class="keyword">return</span> <span class="number">true</span>;
  }

  <span class="keyword">function</span> <span class="name">showPuzzleError</span>(str)
  {
      <span class="comment">//TODO: Nice user visible UI?</span>
      <span class="name">console</span>.<span class="name">log</span>(<span class="name">str</span>);
  }

  <span class="keyword">function</span> <span class="name">loadMap</span>(map)
  {
      <span class="name">puzzlePath</span> <span class="operator">=</span> <span class="name">map</span>;
      var levelComp = <span class="name">Qt</span>.<span class="name">createComponent</span>(<span class="name">puzzlePath</span>);
      <span class="keyword">if</span> (<span class="name">levelComp</span>.<span class="name">status</span> <span class="operator">!=</span> <span class="number">1</span>){
          <span class="name">console</span>.<span class="name">log</span>(<span class="string">&quot;Error loading level&quot;</span>);
          <span class="name">showPuzzleError</span>(<span class="name">levelComp</span>.<span class="name">errorString</span>());
          <span class="keyword">return</span>;
      }
      <span class="name">puzzleLevel</span> <span class="operator">=</span> <span class="name">levelComp</span>.<span class="name">createObject</span>();
      <span class="keyword">if</span> (<span class="name">puzzleLevel</span> <span class="operator">==</span> <span class="number">null</span> <span class="operator">||</span> !<span class="name">puzzleLevel</span>.<span class="name">startingGrid</span> <span class="operator">instanceof</span> <span class="name">Array</span>) {
          <span class="name">showPuzzleError</span>(<span class="string">&quot;Bugger!&quot;</span>);
          <span class="keyword">return</span>;
      }
      <span class="name">gameCanvas</span>.<span class="name">showPuzzleGoal</span>(<span class="name">puzzleLevel</span>.<span class="name">goalText</span>);
      <span class="comment">//showPuzzleGoal should call finishLoadingMap as the next thing it does, before handling more events</span>
  }

  <span class="keyword">function</span> <span class="name">finishLoadingMap</span>()
  {
      <span class="keyword">for</span> (var i in <span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>)
          <span class="keyword">if</span> (! (<span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>[<span class="name">i</span>] <span class="operator">&gt;=</span> <span class="number">0</span> <span class="operator">&amp;&amp;</span> <span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>[<span class="name">i</span>] <span class="operator">&lt;=</span> <span class="number">9</span>) )
              <span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>[<span class="name">i</span>] <span class="operator">=</span> <span class="number">0</span>;
      <span class="comment">//TODO: Don't allow loading larger levels, leads to cheating</span>
      <span class="keyword">while</span> (<span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>.<span class="name">length</span> <span class="operator">&gt;</span> <span class="name">maxIndex</span>) <span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>.<span class="name">shift</span>();
      <span class="keyword">while</span> (<span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>.<span class="name">length</span> <span class="operator">&lt;</span> <span class="name">maxIndex</span>) <span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>.<span class="name">unshift</span>(<span class="number">0</span>);
      <span class="keyword">for</span> (var i in <span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>)
          <span class="keyword">if</span> (<span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>[<span class="name">i</span>] <span class="operator">&gt;</span> <span class="number">0</span>)
              <span class="name">createBlock</span>(<span class="name">i</span> <span class="operator">%</span> <span class="name">maxColumn</span>, <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">i</span> <span class="operator">/</span> <span class="name">maxColumn</span>), <span class="name">puzzleLevel</span>.<span class="name">startingGrid</span>[<span class="name">i</span>] <span class="operator">-</span> <span class="number">1</span>);

      <span class="comment">//### Experimental feature - allow levels to contain arbitrary QML scenes as well!</span>
      <span class="comment">//while (puzzleLevel.children.length)</span>
      <span class="comment">//    puzzleLevel.children[0].parent = gameCanvas;</span>
      <span class="name">gameDuration</span> <span class="operator">=</span> new <span class="name">Date</span>(); <span class="comment">//Don't start until we finish loading</span>
  }

  <span class="keyword">function</span> <span class="name">puzzleVictoryCheck</span>(clearedAll)<span class="comment">//gameOver has also been set if no more moves</span>
  {
      var won = <span class="number">true</span>;
      var soFar = new <span class="name">Date</span>() <span class="operator">-</span> <span class="name">gameDuration</span>;
      <span class="keyword">if</span> (<span class="name">puzzleLevel</span>.<span class="name">scoreTarget</span> <span class="operator">!=</span> -<span class="number">1</span> <span class="operator">&amp;&amp;</span> <span class="name">gameCanvas</span>.<span class="name">score</span> <span class="operator">&lt;</span> <span class="name">puzzleLevel</span>.<span class="name">scoreTarget</span>){
          <span class="name">won</span> <span class="operator">=</span> <span class="number">false</span>;
      } <span class="keyword">if</span> (<span class="name">puzzleLevel</span>.<span class="name">scoreTarget</span> <span class="operator">!=</span> -<span class="number">1</span> <span class="operator">&amp;&amp;</span> <span class="name">gameCanvas</span>.<span class="name">score</span> <span class="operator">&gt;=</span> <span class="name">puzzleLevel</span>.<span class="name">scoreTarget</span> <span class="operator">&amp;&amp;</span> !<span class="name">puzzleLevel</span>.<span class="name">mustClear</span>){
          <span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
      } <span class="keyword">if</span> (<span class="name">puzzleLevel</span>.<span class="name">timeTarget</span> <span class="operator">!=</span> -<span class="number">1</span> <span class="operator">&amp;&amp;</span> <span class="name">soFar</span><span class="operator">/</span><span class="number">1000.0</span> <span class="operator">&gt;</span> <span class="name">puzzleLevel</span>.<span class="name">timeTarget</span>){
          <span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
      } <span class="keyword">if</span> (<span class="name">puzzleLevel</span>.<span class="name">moveTarget</span> <span class="operator">!=</span> -<span class="number">1</span> <span class="operator">&amp;&amp;</span> <span class="name">gameCanvas</span>.<span class="name">moves</span> <span class="operator">&gt;=</span> <span class="name">puzzleLevel</span>.<span class="name">moveTarget</span>){
          <span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
      } <span class="keyword">if</span> (<span class="name">puzzleLevel</span>.<span class="name">mustClear</span> <span class="operator">&amp;&amp;</span> <span class="name">gameOver</span> <span class="operator">&amp;&amp;</span> !<span class="name">clearedAll</span>) {
          <span class="name">won</span> <span class="operator">=</span> <span class="number">false</span>;
      }

      <span class="keyword">if</span> (<span class="name">gameOver</span>) {
          <span class="name">gameCanvas</span>.<span class="name">gameOver</span> <span class="operator">=</span> <span class="number">true</span>;
          <span class="name">gameCanvas</span>.<span class="name">showPuzzleEnd</span>(<span class="name">won</span>);

          <span class="keyword">if</span> (<span class="name">won</span>) {
              <span class="comment">// Store progress</span>
              <span class="name">saveLevelHistory</span>();
          }
      }
  }

  <span class="keyword">function</span> <span class="name">getHighScore</span>()
  {
      var db = <span class="name">Sql</span>.<span class="name">LocalStorage</span>.<span class="name">openDatabaseSync</span>(
          <span class="string">&quot;SameGame&quot;</span>,
          <span class="string">&quot;2.0&quot;</span>,
          <span class="string">&quot;SameGame Local Data&quot;</span>,
          <span class="number">100</span>
      );
      <span class="name">db</span>.<span class="name">transaction</span>(
          <span class="keyword">function</span>(tx) {
              <span class="name">tx</span>.<span class="name">executeSql</span>(<span class="string">'CREATE TABLE IF NOT EXISTS Scores(game TEXT, score NUMBER, gridSize TEXT, time NUMBER)'</span>);
              <span class="comment">// Only show results for the current grid size</span>
              var rs = <span class="name">tx</span>.<span class="name">executeSql</span>(<span class="string">'SELECT * FROM Scores WHERE gridSize = &quot;'</span>
                  <span class="operator">+</span> <span class="name">maxColumn</span> <span class="operator">+</span> <span class="string">&quot;x&quot;</span> <span class="operator">+</span> <span class="name">maxRow</span> <span class="operator">+</span> <span class="string">'&quot; AND game = &quot;'</span> <span class="operator">+</span> <span class="name">gameMode</span> <span class="operator">+</span> <span class="string">'&quot; ORDER BY score desc'</span>);
              <span class="keyword">if</span> (<span class="name">rs</span>.<span class="name">rows</span>.<span class="name">length</span> <span class="operator">&gt;</span> <span class="number">0</span>)
                  <span class="name">gameCanvas</span>.<span class="name">highScore</span> <span class="operator">=</span> <span class="name">rs</span>.<span class="name">rows</span>.<span class="name">item</span>(<span class="number">0</span>).<span class="name">score</span>;
              <span class="keyword">else</span>
                  <span class="name">gameCanvas</span>.<span class="name">highScore</span> <span class="operator">=</span> <span class="number">0</span>;
          }
      );
  }

  <span class="keyword">function</span> <span class="name">saveHighScore</span>(score)
  {
      <span class="comment">// Offline storage</span>
      var db = <span class="name">Sql</span>.<span class="name">LocalStorage</span>.<span class="name">openDatabaseSync</span>(
          <span class="string">&quot;SameGame&quot;</span>,
          <span class="string">&quot;2.0&quot;</span>,
          <span class="string">&quot;SameGame Local Data&quot;</span>,
          <span class="number">100</span>
      );
      var dataStr = <span class="string">&quot;INSERT INTO Scores VALUES(?, ?, ?, ?)&quot;</span>;
      var data = [
          <span class="name">gameMode</span>,
          <span class="name">score</span>,
          <span class="name">maxColumn</span> <span class="operator">+</span> <span class="string">&quot;x&quot;</span> <span class="operator">+</span> <span class="name">maxRow</span>,
          <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">gameDuration</span> <span class="operator">/</span> <span class="number">1000</span>)
      ];
      <span class="keyword">if</span> (<span class="name">score</span> <span class="operator">&gt;=</span> <span class="name">gameCanvas</span>.<span class="name">highScore</span>)<span class="comment">//Update UI field</span>
          <span class="name">gameCanvas</span>.<span class="name">highScore</span> <span class="operator">=</span> <span class="name">score</span>;

      <span class="name">db</span>.<span class="name">transaction</span>(
          <span class="keyword">function</span>(tx) {
              <span class="name">tx</span>.<span class="name">executeSql</span>(<span class="string">'CREATE TABLE IF NOT EXISTS Scores(game TEXT, score NUMBER, gridSize TEXT, time NUMBER)'</span>);
              <span class="name">tx</span>.<span class="name">executeSql</span>(<span class="name">dataStr</span>, <span class="name">data</span>);
          }
      );
  }

  <span class="keyword">function</span> <span class="name">getLevelHistory</span>()
  {
      var db = <span class="name">Sql</span>.<span class="name">LocalStorage</span>.<span class="name">openDatabaseSync</span>(
          <span class="string">&quot;SameGame&quot;</span>,
          <span class="string">&quot;2.0&quot;</span>,
          <span class="string">&quot;SameGame Local Data&quot;</span>,
          <span class="number">100</span>
      );
      <span class="name">db</span>.<span class="name">transaction</span>(
          <span class="keyword">function</span>(tx) {
              <span class="name">tx</span>.<span class="name">executeSql</span>(<span class="string">'CREATE TABLE IF NOT EXISTS Puzzle(level TEXT, score NUMBER, moves NUMBER, time NUMBER)'</span>);
              var rs = <span class="name">tx</span>.<span class="name">executeSql</span>(<span class="string">'SELECT * FROM Puzzle WHERE level = &quot;'</span> <span class="operator">+</span> <span class="name">puzzlePath</span> <span class="operator">+</span> <span class="string">'&quot; ORDER BY score desc'</span>);
              <span class="keyword">if</span> (<span class="name">rs</span>.<span class="name">rows</span>.<span class="name">length</span> <span class="operator">&gt;</span> <span class="number">0</span>) {
                  <span class="name">gameCanvas</span>.<span class="name">puzzleWon</span> <span class="operator">=</span> <span class="number">true</span>;
                  <span class="name">gameCanvas</span>.<span class="name">highScore</span> <span class="operator">=</span> <span class="name">rs</span>.<span class="name">rows</span>.<span class="name">item</span>(<span class="number">0</span>).<span class="name">score</span>;
              } <span class="keyword">else</span> {
                  <span class="name">gameCanvas</span>.<span class="name">puzzleWon</span> <span class="operator">=</span> <span class="number">false</span>;
                  <span class="name">gameCanvas</span>.<span class="name">highScore</span> <span class="operator">=</span> <span class="number">0</span>;
              }
          }
      );
  }

  <span class="keyword">function</span> <span class="name">saveLevelHistory</span>()
  {
      var db = <span class="name">Sql</span>.<span class="name">LocalStorage</span>.<span class="name">openDatabaseSync</span>(
          <span class="string">&quot;SameGame&quot;</span>,
          <span class="string">&quot;2.0&quot;</span>,
          <span class="string">&quot;SameGame Local Data&quot;</span>,
          <span class="number">100</span>
      );
      var dataStr = <span class="string">&quot;INSERT INTO Puzzle VALUES(?, ?, ?, ?)&quot;</span>;
      var data = [
          <span class="name">puzzlePath</span>,
          <span class="name">gameCanvas</span>.<span class="name">score</span>,
          <span class="name">gameCanvas</span>.<span class="name">moves</span>,
          <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">gameDuration</span> <span class="operator">/</span> <span class="number">1000</span>)
      ];
      <span class="name">gameCanvas</span>.<span class="name">puzzleWon</span> <span class="operator">=</span> <span class="number">true</span>;

      <span class="name">db</span>.<span class="name">transaction</span>(
          <span class="keyword">function</span>(tx) {
              <span class="name">tx</span>.<span class="name">executeSql</span>(<span class="string">'CREATE TABLE IF NOT EXISTS Puzzle(level TEXT, score NUMBER, moves NUMBER, time NUMBER)'</span>);
              <span class="name">tx</span>.<span class="name">executeSql</span>(<span class="name">dataStr</span>, <span class="name">data</span>);
          }
      );
  }

  <span class="keyword">function</span> <span class="name">nuke</span>() <span class="comment">//For &quot;Debug mode&quot;</span>
  {
      <span class="keyword">for</span> (var row = 1; <span class="name">row</span> <span class="operator">&lt;=</span> <span class="number">5</span>; row++) {
          <span class="keyword">for</span> (var col = 0; <span class="name">col</span> <span class="operator">&lt;</span> <span class="number">5</span>; col++) {
              <span class="keyword">if</span> (<span class="name">board</span>[<span class="name">index</span>(<span class="name">col</span>, <span class="name">maxRow</span> <span class="operator">-</span> <span class="name">row</span>)] <span class="operator">!=</span> <span class="number">null</span>) {
                  <span class="name">board</span>[<span class="name">index</span>(<span class="name">col</span>, <span class="name">maxRow</span> <span class="operator">-</span> <span class="name">row</span>)].<span class="name">dying</span> <span class="operator">=</span> <span class="number">true</span>;
                  <span class="name">board</span>[<span class="name">index</span>(<span class="name">col</span>, <span class="name">maxRow</span> <span class="operator">-</span> <span class="name">row</span>)] <span class="operator">=</span> <span class="number">null</span>;
              }
          }
      }
      <span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">&quot;multiplayer&quot;</span> <span class="operator">&amp;&amp;</span> <span class="name">gameCanvas</span>.<span class="name">curTurn</span> <span class="operator">==</span> <span class="number">2</span>)
          <span class="name">shuffleUp</span>();
      <span class="keyword">else</span>
          <span class="name">shuffleDown</span>();
      <span class="keyword">if</span> (<span class="name">gameMode</span> <span class="operator">==</span> <span class="string">&quot;endless&quot;</span>)
          <span class="name">refill</span>();
      <span class="keyword">else</span>
          <span class="name">victoryCheck</span>();
  }

</pre>
